package com.lewis.listviewtest;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends ActionBarActivity {

    //private String[] data = {"Apple","Banana","Orange","Watermelon","Pear","Grape","Pineapple",
    //        "Strawberry","Cherry","Mango"};

    private List<Fruit> fruitList =new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initFruits();
        //ArrayAdapter adapter = new ArrayAdapter(MainActivity.this,
        // android.R.layout.simple_list_item_1,data);
        FruitAdapter adapter = new FruitAdapter(MainActivity.this,R.layout.fruit_item,fruitList);
        ListView listView = (ListView)findViewById(R.id.list_view);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Fruit fruit = fruitList.get(position);
                Toast.makeText(MainActivity.this,fruit.getName(),Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 初始化水果数据
     */
    private void initFruits(){
        Fruit apple = new Fruit("Apple",R.mipmap.ic_launcher);
        fruitList.add(apple);
        Fruit banana = new Fruit("banana",R.mipmap.ic_launcher);
        fruitList.add(banana);
        Fruit Orange = new Fruit("Orange",R.mipmap.ic_launcher);
        fruitList.add(Orange);
        Fruit Watermelon = new Fruit("Watermelon",R.mipmap.ic_launcher);
        fruitList.add(Watermelon);
        Fruit Pear = new Fruit("Pear",R.mipmap.ic_launcher);
        fruitList.add(Pear);
        Fruit Grape = new Fruit("Grape",R.mipmap.ic_launcher);
        fruitList.add(Grape);
        Fruit Pineapple = new Fruit("Pineapple",R.mipmap.ic_launcher);
        fruitList.add(Pineapple);
        Fruit Strawberry = new Fruit("Strawberry",R.mipmap.ic_launcher);
        fruitList.add(Strawberry);
        Fruit Cherry = new Fruit("Cherry",R.mipmap.ic_launcher);
        fruitList.add(Cherry);
        Fruit Mango = new Fruit("Mango",R.mipmap.ic_launcher);
        fruitList.add(Mango);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
